package com.group.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.DelFileRequest;
import com.qcloud.cos.request.UploadFileRequest;
import com.qcloud.cos.sign.Credentials;

/**
 * 腾讯云cos储存包
 * @author caoyanan
 * @time 2017年12月17日下午11:41:11
 */
public class QCloudCosUtil {
	
	//常量
	private static long appId = 1252651376;
	private static String secretId = "AKIDtf2GQSZ7qOkusexfuxoX8YovrcCamojT";
	private static String secretKey = "jP0QfiaHmpiPETcbO2VoyymPqHWbVDk7";
    // 设置要操作的bucket
	private static String bucketName = "image";

	private static COSClient cosClient = null;
	
	
	
	/**
	 * 上传文件
	 */
	public static String uploadFile(byte[] bytes,String cosPath) {
		// 初始化秘钥信息
        Credentials cred = new Credentials(appId, secretId, secretKey);
        // 初始化客户端配置
        ClientConfig clientConfig = new ClientConfig();
        // 设置bucket所在的区域，比如华南园区：gz； 华北园区：tj；华东园区：sh ；
        clientConfig.setRegion("cd");
        // 初始化cosClient
        COSClient cosClient = new COSClient(clientConfig, cred);
        
		
		UploadFileRequest request = new UploadFileRequest(bucketName, cosPath, bytes);
		String result = cosClient.uploadFile(request);
		
		cosClient.shutdown();
		return result;
	}
	
 
	/**
	 * 删除文件
	 * @param cosPath
	 */
	public static String deleteFile(String cosPath) {
		// 初始化秘钥信息
		Credentials cred = new Credentials(appId, secretId, secretKey);
		// 初始化客户端配置
		ClientConfig clientConfig = new ClientConfig();
		// 设置bucket所在的区域，比如华南园区：gz； 华北园区：tj；华东园区：sh ；
		clientConfig.setRegion("cd");
		// 初始化cosClient
		COSClient cosClient = new COSClient(clientConfig, cred);
		
		//删除文件
		DelFileRequest delFileRequest = new DelFileRequest(bucketName, cosPath);
		String delFile = cosClient.delFile(delFileRequest);
		
		cosClient.shutdown();
		
		return delFile;
	}
}
/**
 {
 "code":0,
 "message":"SUCCESS",
 "request_id":"NWEzNjk5NDBfMjNiMjU4NjRfNWI0Zl8xOTUxMA==",
 "data":{
 		"access_url":"http://image-1252651376.file.myqcloud.com/2017/12/18/476f5d7db61742d1b6072a3455e65fe9.png",
 		"resource_path":"/1252651376/image/2017/12/18/476f5d7db61742d1b6072a3455e65fe9.png",
 		"source_url":"http://image-1252651376.coscd.myqcloud.com/2017/12/18/476f5d7db61742d1b6072a3455e65fe9.png",
 		"url":"http://cd.file.myqcloud.com/files/v2/1252651376/image/2017/12/18/476f5d7db61742d1b6072a3455e65fe9.png",
 		"vid":"535e9e32b42317a5da4d8c0d960d8aa81513527616"}
 }
		
 */
